﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


using Microsoft.VisualBasic;
using PlanningGIS.Util.Convertor;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 属性异常值检查（所有要素的数字或字符属性不能含有空格、回车、换行或半角字符和全角字符混合情况）
    /// </summary>
    public class FieldExceptionValueCheck : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr,IGeometry pGeometry)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;
            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");
            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);
            

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max ==0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return -1;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;
            
            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                for (int i = 0; i < pFeatCls.Fields.FieldCount ; i++)
                {
                    IField pField = pFeatCls.Fields.get_Field(i);
                    if (!pField.Editable) continue;
                    if ((int)pField.Type > 4) continue;

                    //如果该属性字段为空，正确
                    if (pFeature.get_Value(i) == DBNull.Value || pFeature.get_Value(i) == null) continue;

                    //如果该属性项为纯数字，则认为是正确的
                    string fieldvalue = pFeature.get_Value(i).ToString();
                    if (StringHelper.CanConvert2Double(fieldvalue)) continue;

                    //如果该属性项就是一个空格，则认为是正确的（与数字0表示同一含义）
                    if (fieldvalue == " ") continue;

                    //如果该属性项是一个空字符串，也认为是正确的（与数字0表示同一含义）
                    if (fieldvalue == "") continue;

                    string fieldname = (pField.Name == pField.AliasName) ? pField.Name : pField.Name + "(" + pField.AliasName + ")";

                    if (fieldvalue.IndexOf(' ') != -1)
                    {
                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, fieldname + "字段中有空格");
                        errCount++;
                        break ;
                    }

                    if (fieldvalue.IndexOf('\r') != -1)
                    {
                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, fieldname + "字段中有换行字符");
                        errCount++;
                        break;
                    }

                    if (fieldvalue.IndexOf('\n') != -1)
                    {
                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, fieldname + "字段中有回车字符");
                        errCount++;
                        break;
                    }

                    foreach (char c in fieldvalue)
                    {
                        if (Strings.Asc(c) >= 32 && Strings.Asc(c) <= 127)
                        {
                            RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, fieldname + "字段中有半角字符");
                            errCount++;
                            break;
                        }
                    }
                }
            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("属性异常值检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }
    }
}
